package com.mashibing.servicemap.remote;


import com.mashibing.internalcommon.constant.AmapConfigConstants;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.response.ServiceResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 创建服务
 * @author 史慕尧
 */
@Service
public class ServiceClient {
@Value("${amap.key}")
private String amapKey;
@Autowired
    private RestTemplate restTemplate;

public ResponseResult add(String name){

    // &key=<用户的key>
    // 拼装请求的url
    StringBuilder url = new StringBuilder();
    url.append(AmapConfigConstants.SERVICE_ADD_URL);
    url.append("?");
    url.append("key="+amapKey);
    url.append("&");
    url.append("name="+name);
    ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url.toString(), null, String.class);
    String body = stringResponseEntity.getBody();
    JSONObject jsonObject = JSONObject.fromObject(body);
    JSONObject data = jsonObject.getJSONObject("data");
    String sid = data.getString("sid");
    ServiceResponse serviceResponse=new ServiceResponse();
    serviceResponse.setSid(sid);
    return ResponseResult.success(serviceResponse);
}

}
